//
//  User.m
//  XPhoto
//
//  Created by baboy on 2019/6/26.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "User.h"
#import <BKKit/BKApi.h>
#import "HttpResponse.h"
#import "QiniuConf.h"

@implementation UserRegisterMobileArgs

@end
@implementation User

+ (id)loginWithUserName:(NSString *)userName
               password:(NSString *)password
               callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    NSDictionary *param = @{
                            @"userName": userName,
                            @"password": password
                            };
    return [BKApi request:API_AUTH_LOGIN
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                            error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];    
}

+ (id)refreshTokenWithUser:(User *)user
                  callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    NSDictionary *param = user.authToken;
    return [BKApi request:API_AUTH_REFRESH_TOKEN
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                            error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];    
}

+ (id)signup:(User *)user
    callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    return [BKApi request:API_AUTH_REGISTER_MOBILE
             parameters:user
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];
}

+ (id)sendBindMobileVCode:(NSString *)mobile
                 callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id param = @{  @"mobile": mobile };
    return [BKApi request:API_USER_SEND_BIND_MOBILE_VCODE
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json];
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, nil, error);
               }];
}

+ (id)sendRegisterMobileVCode:(NSString *)mobile
                     callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id param = @{ @"mobile": mobile };
    return [BKApi request:API_USER_SEND_REGISTER_MOBILE_VCODE
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json];
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, nil, error);
               }];
}
+ (id)sendLoginMobileVCode:(NSString *)mobile
                  callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    id param = @{ @"mobile": mobile };
    return [BKApi request:API_USER_SEND_LOGIN_MOBILE_VCODE
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json];
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, nil, error);
               }];
}

+ (id)loginWithMobile:(NSString *)mobile
               vcode:(NSString *)vcode
             callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    NSDictionary *param = @{
                            @"mobile": mobile,
                            @"vcode": vcode
                            };
    return [BKApi request:API_AUTH_LOGIN_BY_VCODE
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                            error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];
}
+ (id)signupWithMobile:(UserRegisterMobileArgs *)args
              callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    return [BKApi request:API_AUTH_REGISTER_MOBILE
             parameters:args
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];
}
+ (id)bindMobile:(NSString *)mobile
           vcode:(NSString *)vcode
        callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id param = @{
                 @"mobile": mobile,
                 @"vcode": vcode
                 };
    return [BKApi request:API_USER_BIND_MOBILE
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json];
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, nil, error);
               }];
}

+ (id)resetPassword:(NSDictionary *)args
           callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    return [BKApi request:API_USER_PROFILE_RESET_PASSWORD
    parameters:args
      callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
          if (!error && json) {
              HttpResponse *response = [HttpResponse responseWithDictionary:json];
              if (![response isSuccess]) {
                  error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
              }
          }
          callback(task, nil, error);
      }];
}
+ (id)update:(User *)user
    callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    return [BKApi request:API_USER_PROFILE_UPDATE
             parameters:user
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json];
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, nil, error);
               }];
}

+ (id)updateUserName:(NSString *)userName
            callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id param = @{@"userName":userName};
    return [BKApi request:API_USER_PROFILE_UPDATE_USER_NAME
    parameters:param
      callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
          if (!error && json) {
              HttpResponse *response = [HttpResponse responseWithDictionary:json];
              if (![response isSuccess]) {
                  error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
              }
          }
          callback(task, nil, error);
      }];
}

+ (id)getUserProfile:(NSInteger)uid
            callback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    id param = @{
                 @"uid": [NSNumber numberWithLong:uid]
                 };
    return [BKApi request:API_USER_PROFILE_GET
             parameters:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];
}
+ (id)getLoginUserProfileWithCallback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    return [BKApi request:API_USER_PROFILE_ME
             parameters:nil
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {                   
                   User *user = nil;
                   if (!error && json) {
                       HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[User class]];
                       if ([response isSuccess]) {
                           user = response.data;
                       }
                       if (![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                       }
                   }
                   callback(task, user, error);
               }];
}
+ (id)updateCover:(PhotoAsset *)photoAsset
         photoUploadProgress:(void(^)(NSString *key, float p))progress
         callback:(void (^)(id _Nonnull, id _Nullable, NSError * _Nullable))callback{

    NSString *path = [NSString stringWithFormat:@"/profile/%ld/cover/%@", [(User*)[User currentUser] uid], [photoAsset.originPath lastPathComponent]];
    [QiniuConf uploadFile:photoAsset.originPath
                 fileName:path
                 progress:progress
               completion:^(QNResponseInfo * _Nonnull info, NSString * _Nonnull key, NSDictionary * _Nonnull resp) {
        
        NSString *cover = [NSString stringWithFormat:@"%@/%@",[QiniuConf defaultConf].domain, key];
        id param = @{
            @"cover":cover
        };
        [BKApi request:API_USER_PROFILE_UPDATE_COVER
                   parameters:param
                     callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
            
            if (!error && json) {
                HttpResponse *response = [HttpResponse responseWithDictionary:json];
                if (![response isSuccess]) {
                    error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                }
            }
            callback(task, nil, error);
        }];
    }];
    return nil;
}
+ (id)updateAvatar:(PhotoAsset *)photoAsset
         photoUploadProgress:(void(^)(NSString *key, float p))progress
         callback:(void (^)(id _Nonnull, id _Nullable, NSError * _Nullable))callback{

    [self uploadAvatar:photoAsset
   photoUploadProgress:progress
              callback:^(id  _Nonnull info, id  _Nullable param, NSError * _Nullable error) {
        [BKApi request:API_USER_PROFILE_UPDATE_AVATAR
                   parameters:param
                     callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
            
            if (!error && json) {
                HttpResponse *response = [HttpResponse responseWithDictionary:json];
                if (![response isSuccess]) {
                    error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                }
            }
            callback(task, nil, error);
        }];
    }];
    return nil;
}

+ (id)uploadAvatar:(PhotoAsset *)photoAsset
photoUploadProgress:(void(^)(NSString *key, float p))progress
          callback:(nullable void (^)(id _Nonnull info, id _Nullable ret,  NSError * _Nullable error))callback{
    NSString *path = [NSString stringWithFormat:@"/profile/%ld/avatar/%@", [(User*)[User currentUser] uid], [photoAsset.originPath lastPathComponent]];
    [QiniuConf uploadFile:photoAsset.originPath
                 fileName:path
                 progress:progress
               completion:^(QNResponseInfo * _Nonnull info, NSString * _Nonnull key, NSDictionary * _Nonnull resp) {
        
        NSString *avatar = [NSString stringWithFormat:@"%@/%@",[QiniuConf defaultConf].domain, key];
        NSString *avatarThumb = [NSString stringWithFormat:@"%@-avatarThumb", avatar];
        id ret = @{
            @"avatar":avatar,
            @"avatarThumb":avatarThumb
        };
        callback(info, ret, nil);
    }];
    return nil;
}
@end
